package cn.dnui_zll731.dnuifood_731zlll.controller;


import android.content.Intent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.Date;

import cn.dnui_zll731.dnuifood_731zlll.bean.Result;
import cn.dnui_zll731.dnuifood_731zlll.common.Constants;
import cn.dnui_zll731.dnuifood_731zlll.R;
import cn.dnui_zll731.dnuifood_731zlll.bean.UserBean;
import cn.dnui_zll731.dnuifood_731zlll.listener.RetrofitListener;
import cn.dnui_zll731.dnuifood_731zlll.model.ShopModel;
import cn.dnui_zll731.dnuifood_731zlll.model.UserModel;

public class PurchaseActivity extends BaseActivity implements RetrofitListener {
    private Intent intent;
    private String shop_id,food_id,food_name,price,adress;
    private TextView P_food_name,P_food_price,P_user_name,P_phone,P_address,P_shuliang;
    private ImageButton P_fanhui;
    private ImageView P_img;
    @Override
    protected int initlayout() {
        return R.layout.activity_purchase;
    }
    @Override
    void initViews() {
        //初始化控件View
        P_fanhui=findViewById(R.id.P_fanhui);
        P_img = findViewById(R.id.P_pic);
        P_food_name = findViewById(R.id.P_food_name);
        P_food_price = findViewById(R.id.P_food_price);
        P_user_name = findViewById(R.id.P_username);
        P_phone = findViewById(R.id.P_phone);
        P_address = findViewById(R.id.P_address);
        P_shuliang = findViewById(R.id.P_shuliang);
    }

    @Override
    void initEvents() {
        P_fanhui.setOnClickListener(new View.OnClickListener() {//返回
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        findViewById(R.id.P_tijiao).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //提交订单
                purchase();
            }
        });
    }

    @Override
    void initData() {
        intent =getIntent();
        food_id = intent.getStringExtra("food_id");
        shop_id = intent.getStringExtra("shop_id");
        food_name = intent.getStringExtra("food_name");
        price = intent.getStringExtra("price");
        String pic = intent.getStringExtra("pic");
        //获取登录用户的基本信息
        showUserInfo();
        //获取当前店铺的信息
        // showShopInfo();
        //设置送餐时间
        //setSpinner();

        Picasso.with(this).load(Constants.BASE_URL + pic).into(P_img);
        P_food_name.setText(food_name);
        P_food_price.setText(price);
    }

    private void showUserInfo() {      //-----------获取登录用户的基本信息
        UserModel userModel = new UserModel();
        userModel.getUserById(getUser_id(),this);

    }

    private void showShopInfo() {    //------------获取当前店铺的信息
        ShopModel shopModel = new ShopModel();
        shopModel.getShopById(shop_id,this);
    }

    private void setSpinner(Spinner sp,Date date) {         //----------设置送餐时间
//        Date[] dates = new Date[5];
//        for (int i=0 ; i<dates.length ; i++){
//            dates[i] = new Date(date.getTime()+(long)30*60*1000*(i+1));
//        }
//        final String[] mItems = new String[dates.length-1];
//        for (int i = 0; i<mItems.length;i++){
//            mItems[i] = dates[i].getHours()+":"+formateMinute(dates[i].getMinutes())+
//                    "----"+dates[i+1].getHours()+":"+formateMinute(dates[i+1].getMinutes());
//        }
    }

    @Override
    public void onSuccess(Object object, int flag) {
        switch (flag){
            case Constants.GETUSERBYID:
                P_user_name.setText(((UserBean)object).getUsername()+"(先生) ");
                P_phone.setText(((UserBean)object).getMobilenum());
                P_address.setText(((UserBean)object).getAddress());
                adress=((UserBean)object).getAddress();
                break;
            case Constants.GETSHOPBYID:

                break;
            case Constants.INSERTORDER:
                Result status = (Result)object;
                if (status.getSuccess().equals("1")){
                    showToast("恭喜，支付成功！");
                }else {
                    showToast("支付失败");
                }
                break;
        }
    }

    @Override
    public void onFalure() {
        showToast("网络错误");
    }

    private void purchase(){
        UserModel userModel = new UserModel();
        String shuliang=P_shuliang.getText().toString();
        userModel.insertOrder(getUser_id(),food_id,shuliang,price,"11:00-11:30",adress,this);
    }
}